package com.yoomiito.app.adapter.home.reommend;

import android.content.Context;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.RelativeSizeSpan;
import android.text.style.StrikethroughSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.yoomiito.app.R;
import com.yoomiito.app.base.App;
import com.yoomiito.app.model.bean.GoodsInfo;
import com.yoomiito.app.utils.ImageLoader;
import com.yoomiito.app.utils.ResUtil;
import com.yoomiito.app.utils.SpannableUtils;
import com.yoomiito.app.utils.Type;
import com.yoomiito.app.utils.UserInfoConfig;
import com.yoomiito.app.widget.CouponView;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

/**
 * 首页---活动
 */
public class HomeRecommendAdapter_1 extends DelegateAdapter.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    private LayoutHelper mLayoutHelper;
    private List<GoodsInfo> datas;
    private OnItemClickLisenter mOnItemClickLisenter;
    public HomeRecommendAdapter_1(Context context, LayoutHelper layoutHelper, List<GoodsInfo> datas) {
        mContext = context;
        mLayoutHelper = layoutHelper;
        this.datas = datas;
    }

    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return mLayoutHelper;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_recommend_goods, parent, false);
        return new ItemViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
        ItemViewHolder itemViewHolder = (ItemViewHolder) holder;
        GoodsInfo info = datas.get(position);
        //设置title
//        setTitle(itemViewHolder.titleTv,info.getTitle(), DataUtils.INSTANCE.ivType(Integer.valueOf(info.getSource())));
        //优惠券
        if (SpannableUtils.isEmptyOr0(info.getCouponPrice())){
            itemViewHolder.mCouponView.setHeaderTextVisible(false);
            itemViewHolder.mCouponView.setText("特价精选");
        }else {
            itemViewHolder.mCouponView.setHeaderTextVisible(true);
            itemViewHolder.mCouponView.setText("¥"+info.getCouponPrice());
        }
        itemViewHolder.mCouponView.setVisibility(View.VISIBLE);

//        itemViewHolder.mGoodsSizeTv.setText("月销"+SpannableUtils.saleNum(info.getSalesNum()));
        //奖--佣金  会员以上显示  分享赚同奖
        if (App.mAppConfig.getIsLogin()){
            itemViewHolder.mAwardView.setVisibility(UserInfoConfig.getRoleId()< Type.LevelType.VIP_7||SpannableUtils.isEmptyOr0(info.getBuyAwardPrice())?View.GONE:View.VISIBLE);
            itemViewHolder.mAwardView.setText("¥"+info.getBuyAwardPrice());
        }else {
            itemViewHolder.mAwardView.setVisibility(View.GONE);
        }

        String goodsPrice = info.getDiscountPrice();
        String oldPrice = info.getZkFinalPrice();
        String price = SpannableUtils.isEmptyOr0(info.getCouponPrice())?("¥" + goodsPrice+" "):("¥" + goodsPrice + " ¥" + oldPrice);
        SpannableString spannableString = new SpannableString(price);
        spannableString.setSpan(new RelativeSizeSpan(0.65f),0,1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
//        spannableString.setSpan(new RelativeSizeSpan(0.65f),(goodsPrice.contains(".")?goodsPrice.indexOf("."):goodsPrice.length())+1,price.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
//        spannableString.setSpan(new ForegroundColorSpan(ResUtil.getColor(R.color.color_balk_999999)),goodsPrice.length()+2,price.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        StrikethroughSpan strikethroughSpan = new StrikethroughSpan();
//        spannableString.setSpan(strikethroughSpan, goodsPrice.length()+2, price.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
//        itemViewHolder.mGoodsPriceTv.setText(spannableString);

        ImageLoader.getInstance().into(mContext, info.getImageUrl(), 4,itemViewHolder.goodsIv, R.drawable.goods_default);
        itemViewHolder.mView.setOnClickListener(v -> {
            if (mOnItemClickLisenter != null) {
                mOnItemClickLisenter.onClick(position,false);
            }
        });
    }

    private void setTitle(TextView tv,String str,int ivResId) {
        tv.setText("");
//        SpannableString spanText = new SpannableString(""+str);
//        if (ivResId!=0){
//            Drawable d = mContext.getDrawable(ivResId);
//            d.setBounds(0, 0, DensityUtil.dp2px(22), DensityUtil.dp2px(14));
//            spanText.setSpan(new ImageSpan(d), 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
//        }
        tv.append(SpannableUtils.getIconAndText(str, ResUtil.getDrawable(ivResId)));
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    @Override
    public int getItemViewType(int position) {
        return 5;
    }

    private void setGiftAndCoupon(View view, TextView tv, String value) {
//        boolean isCoupon = TextUtils.isEmpty(value)||"0".equals(value)||"0.0".equals(value)||"0.00".equals(value);
        boolean isCoupon = SpannableUtils.isEmptyOr0(value);
        view.setVisibility(isCoupon ? View.GONE : View.VISIBLE);
        if (!isCoupon) tv.setText("¥" + value);
    }

    private class ItemViewHolder extends RecyclerView.ViewHolder {

//        private TextView shareTv;
        private TextView titleTv;//商品名称
        private ImageView goodsIv;//商品图
        private View mView;
        private TextView mGoodsSizeTv;
        private TextView mGoodsPriceTv;
        private CouponView mAwardView;
        private CouponView mCouponView;
        public ItemViewHolder(View itemView) {
            super(itemView);
            mView = itemView;
            goodsIv = itemView.findViewById(R.id.item_goods_img);
            titleTv = itemView.findViewById(R.id.goodsTitle);
            mGoodsSizeTv = itemView.findViewById(R.id.goodsSize);
            mGoodsPriceTv = itemView.findViewById(R.id.goodsPrice);
            mAwardView = itemView.findViewById(R.id.awardView);
            mCouponView = itemView.findViewById(R.id.couponView);
        }
    }

    public void setOnItemClickLisenter(OnItemClickLisenter onItemClickLisenter) {
        mOnItemClickLisenter = onItemClickLisenter;
    }

    public interface OnItemClickLisenter {
        void onClick(int position, boolean isShare);
    }
}
